package pl.gpms.server.persistance.dao;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import pl.gpms.client.dto.SymptomDTO;
import pl.gpms.server.persistance.map.Symptom;
import pl.gpms.server.persistance.tools.HibernateUtil;

public class SymptomDao {
	
	public static boolean isUsed( String symptomName ) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int symptomsCount = 0;
		boolean result = false;
		
		try {
			transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria( Symptom.class );
			criteria.add( Restrictions.like( "symptomName", symptomName ) );
			symptomsCount = criteria.list().size();
		} catch( HibernateException e ) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		if( symptomsCount > 0 )
			result = true;
		
		return result;
	}
	
	public SymptomDTO createSymptomDto(Symptom symptom) {
		EstimateDao estimateDao = new EstimateDao();
		return new SymptomDTO( estimateDao.createEstimateDTO(symptom.getEstimate()), 
				               symptom.getSymptomName(), 
				               symptom.getSymptomConfidence(), 
				               symptom.getId());
	}
}
